﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web.Admin.Feedback
{
    public partial class SurveyItemList : PageBase
    {
        int _SurveyId;
        BLL.SurveyItem bll = new BLL.SurveyItem();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("feedback", "read");

            int.TryParse(Request.QueryString["surveyId"], out _SurveyId);
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            string strWhere = "IsActive=1";
            if (_SurveyId != 0)
            {
                strWhere += " and SurveyId=" + _SurveyId;
            }
            strWhere += " order by OrderNo asc";
            DataSet ds = bll.GetList(strWhere);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Check("feedback", "del");

            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                //bll.DeleteByIds(ids);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            editT.Visible = true;
            txtTitle.Text = "";
            txtOrderNo.Text = "";
            hidSurveyOptionId.Value = "";
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ModifyItem")
            {
                editT.Visible = true;
                Model.SurveyItem soModel = bll.GetModel(int.Parse(e.CommandArgument.ToString()));
                txtTitle.Text = soModel.ItemTitle;
                txtOrderNo.Text = soModel.OrderNo;
                ddlItemType.SelectedValue = soModel.ItemType.ToString();
                hidSurveyOptionId.Value = soModel.SurveyItemId.ToString();
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (hidSurveyOptionId.Value != "")
            {
                Model.SurveyItem soModel = bll.GetModel(int.Parse(hidSurveyOptionId.Value));
                soModel.ItemTitle = txtTitle.Text.Trim();
                soModel.OrderNo = txtOrderNo.Text.Trim();
                soModel.UpdateTime = DateTime.Now;
                soModel.ItemType = int.Parse(ddlItemType.SelectedValue);
                bll.Update(soModel);
            }
            else
            {
                Model.SurveyItem soModel = new Model.SurveyItem();
                soModel.SurveyId = _SurveyId;
                soModel.ItemTitle = txtTitle.Text.Trim();
                soModel.OrderNo = txtOrderNo.Text.Trim();
                soModel.ItemType = int.Parse(ddlItemType.SelectedValue);
                soModel.CreateTime = DateTime.Now;
                soModel.UpdateTime = DateTime.Now;
                soModel.IsActive = 1;
                bll.Add(soModel);
            }

            BindData();
            editT.Visible = false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            editT.Visible = false;
            txtTitle.Text = "";
            txtOrderNo.Text = "";
            ddlItemType.SelectedValue = "-1";
            hidSurveyOptionId.Value = "";
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("SurveyList.aspx");
        }
    }
}